﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Nservice.Sales.Messages;
using NServiceBus;


namespace Nservice.Api.Controllers
{
    [RoutePrefix("api/Order")]
    public class OrderController : ApiController
    {
        // GET api/<controller>
        string statusReponse = "";

        [Route("")]
        public PlaceOrder Get()
        {
            var order = new PlaceOrder
            {
                OrderId = Guid.NewGuid(),
                CustomerId = Guid.NewGuid(),
                ProductId = Guid.NewGuid()
            };

            WebApiApplication.bus.Send(order);

            return order;
        }

        // GET api/<controller>/5
        [HttpGet]
        [Route("{id:int}")]
        public string Get(int id)
        {
            return statusReponse;
        }

        // GET api/<controller>/5
        
        [Route("Cancel/{id:int}")]
        [HttpGet]
        public CancelOrder CandelOrder(int id)
        {
            

            var canceledOrder = new CancelOrder
            {
                OrderId = Guid.NewGuid()
            };

            WebApiApplication.bus.Send(canceledOrder).Register(status =>
            {
                statusReponse = Enum.GetName(typeof(CommandStatus), status.ErrorCode);
                statusReponse = statusReponse;
            });

            return canceledOrder;
        }

        // POST api/<controller>
        public void Post([FromBody]string value)
        {
        }

        // PUT api/<controller>/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
        }

    }
}